package com.dream.common.utils;

import com.dream.common.constans.Constants;
import com.dream.common.constans.RedisKeysDefine;
import com.dream.entity.BaseConfig;
import com.dream.entity.MemberBean;
import com.dream.entity.PlayRuleBean;
import com.dream.entity.RiMoneyBean;
import com.dream.service.basic.BaseServiceHelper;

import java.util.ArrayList;
import java.util.List;

public class RedisCacheUtils {
    public static List<MemberBean> getUserListByType(String user, int memberType) {
        List<MemberBean> pickerUserList = Constants.memberUserListMap.get(RedisKeysDefine.MEMBER_LIST + user + "--" + memberType);
        if (pickerUserList != null) {
            return pickerUserList;
        }
        pickerUserList = BaseServiceHelper.getRedisCacheService().getObjectList(RedisKeysDefine.MEMBER_LIST + user + "--" + memberType, MemberBean.class);
        if (pickerUserList != null) {
            Constants.memberUserListMap.put(RedisKeysDefine.MEMBER_LIST + user + "--" + memberType, pickerUserList);
            return pickerUserList;
        }
        return new ArrayList<>();
    }

    public static void setUserListByType(String user, int memberType, List<MemberBean> memberBeanList) {
        Constants.memberUserListMap.put(RedisKeysDefine.MEMBER_LIST + user + "--" + memberType, memberBeanList);
        BaseServiceHelper.getRedisCacheService().setObject(RedisKeysDefine.MEMBER_LIST + user + "--" + memberType, memberBeanList);
    }

    public static PlayRuleBean getPlayRule(String key) {
        PlayRuleBean playRuleBean = Constants.playRuleMap.get(key);
        if (playRuleBean != null) {
            return playRuleBean;
        }
        playRuleBean = BaseServiceHelper.getRedisCacheService().getObject(key, PlayRuleBean.class);
        if (playRuleBean != null) {
            Constants.playRuleMap.put(key, playRuleBean);
            return playRuleBean;
        }
        return new PlayRuleBean();
    }

    public static void setPlayRule(String key, PlayRuleBean playRuleBean) {
        Constants.playRuleMap.put(key, playRuleBean);
        BaseServiceHelper.getRedisCacheService().setObject(key, playRuleBean);
    }

    public static RiMoneyBean getRiMoneyBean(String key) {
        RiMoneyBean riMoneyBean = Constants.riMoneyMap.get(key);
        if (riMoneyBean != null) {
            return riMoneyBean;
        }
        riMoneyBean = BaseServiceHelper.getRedisCacheService().getObject(key, RiMoneyBean.class);
        if (riMoneyBean != null) {
            Constants.riMoneyMap.put(key, riMoneyBean);
            return riMoneyBean;
        }
        return null;
    }

    public static void setRiMoneyBean(String user, RiMoneyBean riMoneyBean) {
        Constants.riMoneyMap.put(RedisKeysDefine.BASE_PLAY_RI + user, riMoneyBean);
        BaseServiceHelper.getRedisCacheService().setObject(RedisKeysDefine.BASE_PLAY_RI + user, riMoneyBean);

    }

    public static MemberBean getNoDead(String user) {
        MemberBean noDead = Constants.noDeadMap.get(user);
        if (noDead != null) {
            return noDead;
        }
        noDead = BaseServiceHelper.getRedisCacheService().getObject(RedisKeysDefine.NO_DEAD_INFO + user, MemberBean.class);
        if (noDead != null) {
            Constants.noDeadMap.put(user, noDead);
            return noDead;
        }
        return null;
    }

    public static void setNoDead(String user, MemberBean noDead) {
        Constants.noDeadMap.put(user, noDead);
        BaseServiceHelper.getRedisCacheService().setObject(RedisKeysDefine.NO_DEAD_INFO + user, noDead);
    }

    public static BaseConfig getBaseConfig(String user) {
        BaseConfig baseConfig = Constants.baseConfigMap.get(user);
        if (baseConfig != null) {
            return baseConfig;
        }
        baseConfig = BaseServiceHelper.getRedisCacheService().getObject(RedisKeysDefine.BASE_CONFIG + user, BaseConfig.class);
        if (baseConfig != null) {
            Constants.baseConfigMap.put(user, baseConfig);
            return baseConfig;
        }
        return new BaseConfig();
    }

    public static void setBaseConfig(String user, BaseConfig baseConfig) {
        Constants.baseConfigMap.put(user, baseConfig);
        BaseServiceHelper.getRedisCacheService().setObject(RedisKeysDefine.BASE_CONFIG + user, baseConfig);

    }
}
